package com.engine.modules.ytht.controller;

import com.engine.common.baseEntity.ResponseEntity.ResponseMsg;
import com.engine.common.baseEntity.jsonResponse.JsonResponseEntity;
import com.engine.common.constant.CommonConstant;
import com.engine.common.util.CacheUtil.MyCacheUtil;
import com.engine.init.Globle;
import com.engine.init.InitDataList;
import com.engine.modules.ytht.entity.LocationEntity;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.geolatte.geom.M;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author FY
 * @version 1.0.0
 * @ClassName SystemController.java
 * @Description TODO
 * @createTime 2021年12月11日
 */
@RestController
@Api(tags = "6、系统相关接口")
@ApiSort(value = 6)
@CrossOrigin
@RequestMapping("/System")
@Slf4j
public class SystemController {

    @Autowired
    CacheManager cacheManager;

    @Value("${spring.profiles.active}")
    public String Env;

    @ApiOperation(value = "清除缓存", notes = "清除缓存")
    @GetMapping(value = "/CleanCache")
    @ApiOperationSupport(order = 1)
    public ResponseMsg CleanCache() {
        try {
            log.info(Env);
            MyCacheUtil.cleanAll(cacheManager,"vectorTilesCache");
            MyCacheUtil.cleanAll(cacheManager,"geoJsonCache");
            MyCacheUtil.cleanAll(cacheManager,"locationCache");
            MyCacheUtil.cleanAll(cacheManager,"dataCache");
            log.info("缓存清除成功！");
            return ResponseMsg.success("缓存清除成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResponseMsg.error("系统错误，e：" + e.getMessage());
        }
    }

    @ApiOperation(value="刷新引擎表控制",notes= "刷新引擎表控制，会重新读取全部表配置文件")
    @GetMapping(value = "/refresh")
    public String refresh(HttpServletResponse response) {
        try {
            String string = InitDataList.initAndRefresh();
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(true, CommonConstant.SC_OK_200, "初始化成功", string, 0, response);
            return jsonResponseEntity.getJsonString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            JsonResponseEntity jsonResponseEntity = new JsonResponseEntity(false, CommonConstant.SC_BK_ERROR, "查询出错，e：" + e.getMessage(), response);
            return jsonResponseEntity.getJsonString();
        }
    }
}
